package com.libcommon.timefromatter

import com.blankj.utilcode.util.StringUtils
import com.libcommon.R
import java.util.Calendar
import java.util.Date

/**
 * Created by walkingMen on 2018/5/17.
 */
object MessageDateFormatter : DateFormatter {
    /**
     * 格式化日期
     */
    override fun format(date: Date): String {
        return formatTime(date.time)
    }

    /**
     * 格式化时间显示
     */
    fun formatTime(timeTamp: Long): String {
        val result: String

        val todayCalendar = Calendar.getInstance()

        val otherCalendar = Calendar.getInstance()
        otherCalendar.timeInMillis = timeTamp

        val year = otherCalendar.get(Calendar.YEAR)
        val month = otherCalendar.get(Calendar.MONTH)
        val day = otherCalendar.get(Calendar.DAY_OF_MONTH)
        val hour = otherCalendar.get(Calendar.HOUR_OF_DAY)

        val amOrPm = when {
            hour in 0..12 -> "AM $hour"
            else -> "PM ${hour - 12}"
        }
        val minute = otherCalendar.get(Calendar.MINUTE)

        result = if (year == todayCalendar.get(Calendar.YEAR)) {
            if (month == todayCalendar.get(Calendar.MONTH)) {
                when (todayCalendar.get(Calendar.DAY_OF_MONTH) - day) {
                    //同一天
                    0 -> "$amOrPm:${format2d(minute)}"
                    //相差一天 昨天
                    1 -> "${StringUtils.getString(R.string.yesterday)} $amOrPm:${format2d(minute)}"
                    //相差多天
                    else -> "${format2d(month + 1)}-${format2d(day)} $amOrPm:${format2d(minute)}"
                }
            } else {
                "${format2d(month + 1)}-${format2d(day)} $amOrPm:${format2d(minute)}"
            }
        } else {
            "${year}-${format2d(month + 1)}-${format2d(day)} $amOrPm:${format2d(minute)}"
        }
        return result
    }

    /**
     * 格式化时间显示
     */
    fun formatTime2(timeTamp: Long): String {
        val result: String

        val todayCalendar = Calendar.getInstance()

        val otherCalendar = Calendar.getInstance()
        otherCalendar.timeInMillis = timeTamp

        val year = otherCalendar.get(Calendar.YEAR)
        val month = otherCalendar.get(Calendar.MONTH)
        val day = otherCalendar.get(Calendar.DAY_OF_MONTH)
        val hour = otherCalendar.get(Calendar.HOUR_OF_DAY)

        val amOrPm = when {
            hour in 0..12 -> "AM $hour"
            else -> "PM ${hour - 12}"
        }
        val minute = otherCalendar.get(Calendar.MINUTE)

        result = if (year == todayCalendar.get(Calendar.YEAR)) {
            if (month == todayCalendar.get(Calendar.MONTH)) {
                when (todayCalendar.get(Calendar.DAY_OF_MONTH) - day) {
                    //同一天
                    0 -> "$amOrPm:${format2d(minute)}"
                    //相差一天 昨天
                    1 -> "${year}-${format2d(month + 1)}-${format2d(day)}"
                    //相差多天
                    else ->  "${year}-${format2d(month + 1)}-${format2d(day)}"
                }
            } else {
                "${year}-${format2d(month + 1)}-${format2d(day)}"
            }
        } else {
            "${year}-${format2d(month + 1)}-${format2d(day)}"
        }
        return result
    }
}
